home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / casm / au116-as.exe / FIND.CPP < prev    next >
C/C++ Source or Header  |  1994-12-13  |  6KB  |  218 lines

  1. // FIND.CPP                                  1          1    6666
  2. // Dave Harris                                11         11   6
  3. // Compiled using Borland C++ ver 3.1       1 1        1 1   6666
  4. // 03-03-94                                  1     ..   1   6   6
  5. //                                           11111 .. 11111  666
  6. ////////////////////////////////////////////////////////////////////////
  7.  
  8. #include "au.hpp"
  9.  
  10. /*********************************************************************/
  11. /* Define Statements */
  12. /*********************/
  13.  
  14. #define PROGRAM "FIND"    // Name of module
  15.  
  16. /*********************************************************************/
  17.  
  18. typedef struct
  19. {
  20.     char match_pattern[32];
  21.     int  match_regular;
  22.     int  number_matched;
  23.     long number_checked;
  24.  
  25.     char find_non_dos;
  26.     char find_paths;
  27.     char one_per_archive;
  28.     char quit_after_first;
  29.     BYTE verbose;
  30. } FIND_INFO;
  31.  
  32. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  33. static void find_one(AU *au, char *file_name, ARC_HANDLE *arc_handle)
  34. {
  35.     int ret_code;
  36.     ARC_RECORD record;
  37.     FIND_INFO *in = (FIND_INFO *)au->info;
  38.     BOOLEAN found = FALSE;
  39.  
  40.     char full_name[FLENGTH];
  41.  
  42.     build_fname(full_name, au->source_directory, file_name);
  43.  
  44.     au->number_processed++;
  45.     for (;;)
  46.     {
  47.         ret_code = arc_handle->get_record(au, &record);
  48.         if (ret_code == EOF || ret_code == -3)
  49.             break;
  50.         else if (ret_code == -2)
  51.         {
  52.             add_to_bad_list(au, au->source_directory, file_name);
  53.             return;
  54.         }
  55.         in->number_checked++;
  56.         if (wildcard_compare(au, record.name, in->match_pattern))
  57.         {
  58.             if ((in->find_non_dos == 0 ||
  59.                  (in->find_non_dos == ON  && !is_dos_name(record.name)) ||
  60.                  (in->find_non_dos == OFF && is_dos_name(record.name))
  61.                 ) &&
  62.                 (in->find_paths == 0 ||
  63.                  (in->find_paths == ON    && record.path[0] != '\0') ||
  64.                  (in->find_paths == OFF && record.path[0] == '\0')
  65.                 )
  66.                )
  67.             {
  68.                 found = TRUE;
  69.                 if (in->one_per_archive == ON)
  70.                     au_printf(au, "@?1%s@?H\n", full_name);
  71.                 else
  72.                     au_printf(au, "@?1%s@?H : @?B%s@?H\n", full_name, record.name);
  73.                 if (in->quit_after_first == ON)
  74.                 {
  75.                     au->cur_directory[0] = '\0';
  76.                     exit(0);
  77.                 }
  78.                 in->number_matched++;
  79.                 if (in->one_per_archive == ON)
  80.                     return;
  81.             }
  82.         }
  83.     }
  84.     if (in->verbose && !found)
  85.         au_printf(au, "@?1%s@?H (Not Found)\n", full_name);
  86. }
  87. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  88. static int find(AU *au, char *file_name)
  89. {
  90.     ARC_HANDLE arc_handle;
  91.     FIND_INFO *in = (FIND_INFO *)au->info;
  92.  
  93.     check_for_key();
  94.  
  95.     if ((in->match_regular == ON || in->match_regular == ONLY)
  96.         && wildcard_compare(au, file_name, in->match_pattern))
  97.     {
  98.         char full_name[FLENGTH];
  99.         build_fname(full_name, au->source_directory, file_name);
  100.         au_printf(au, "@?1%s@?H\n", full_name);
  101.         if (in->quit_after_first == ON)
  102.         {
  103.             au->cur_directory[0] = '\0';
  104.             exit(0);
  105.         }
  106.         in->number_matched++;
  107.     }
  108.  
  109.     if (in->match_regular != ONLY)
  110.     {
  111.         arc_handle.init(au, file_name);
  112.         if (arc_handle.type > 0)
  113.             find_one(au, file_name, &arc_handle);
  114.         else if (in->match_regular == ON)
  115.             au->number_processed++;
  116.         arc_handle.deinit(au);
  117.     }
  118.     return 0;
  119. }
  120. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  121. static BYTE parse_comm_line(AU *au, char option, char *cur_argv,
  122.                             PARSE_TYPE type)
  123. {
  124.     FIND_INFO *in = (FIND_INFO *)au->info;
  125.  
  126.     switch (type)
  127.     {
  128.     case PARSE_SINGLE_OPTION:
  129.         if (option == 'V')
  130.         {
  131.             in->verbose = TRUE;
  132.             return TRUE;
  133.         }
  134.         return FALSE;
  135.     case PARSE_PARAM_OPTION:
  136.         switch (option)
  137.         {
  138.         case 'P':
  139.             if (toupper(*cur_argv) == 'A')
  140.             {
  141.                 strcpy(au->curOpt, "-PA");
  142.                 au->curVal = cur_argv+1;
  143.                 in->find_paths = get_value(au, OFF | ON);
  144.             }
  145.             break;
  146.         case 'R':
  147.             in->match_regular = get_value(au, OFF | ON | ONLY);
  148.             break;
  149.         case 'Q':
  150.             in->quit_after_first = get_value(au, OFF | ON);
  151.             break;
  152.         case 'N':
  153.             in->find_non_dos = get_value(au, OFF |ON);
  154.             break;
  155.         case 'M':
  156.             in->one_per_archive = get_value(au, OFF |ON);
  157.             break;
  158.         case '?':
  159.             au_syntax_message(au, "Find");
  160.             au_printf(au,
  161.                "[@?3options@?H] [@?1filespec(s)@?H] @?Bmatch_pattern@?H\n\n");
  162.             au_param_heading(au);
  163.             au_printf(au,
  164.                 "@?3-R@?Hon|off|only    match Regular Files\n"
  165.                 "@?3-M@?Hon|off         Mention only the archive file\n"
  166.                 "@?3-Q@?Hon|off         Quit After first and change to found directory\n"
  167.                 "\n"
  168.                 "@?7Filters on the searches:\n"
  169.                 "------------------------\n"
  170.                 "@?3-N@?Hon|off         Find Non-dos file names\n"
  171.                 "@?3-PA@?Hon|off        Find archive files with paths\n"
  172.                 "@?3-V@?H               Verbose listing of files as processed\n");
  173.             exit (0);
  174.         default:
  175.             au_invalid_option(au, PROGRAM, option);
  176.         }
  177.         return TRUE;
  178.     case PARSE_FILESPEC:
  179.         if (in->match_pattern[0] != '\0')
  180.             au->process_list.add(in->match_pattern);
  181.         strcpy(in->match_pattern, cur_argv);
  182.         return TRUE;
  183.     case PARSE_POST_CHECK:
  184.         if (in->match_pattern[0] == '\0')
  185.         {
  186.             au_printf_error(au, "No match pattern specified");
  187.             exit(1);
  188.         }
  189.         return TRUE;
  190.     }
  191.     return FALSE;
  192. }
  193. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  194. int main_find(AU *au, int argc, char *argv[])
  195. {
  196.     FIND_INFO *in;
  197.  
  198.     in = new FIND_INFO;
  199.     memset(in, '\0', sizeof(FIND_INFO));
  200.     au->info = in;
  201.     in->match_regular = OFF;
  202.  
  203.     ReadGlobalCFGInfo(au, au->cfg_file, PROGRAM, NULL);
  204.     generic_parse_comm_line(au, argc, argv, parse_comm_line);
  205.     process_files(au, find);
  206.  
  207.     if (!au->no_extra)
  208.     {
  209.         au_printf_c(au, 15, "\nFiles Searched = %d\n", au->number_processed);
  210.         if (in->match_regular != ONLY)
  211.             au_printf_c(au, 15, "Internal files = %ld\n", in->number_checked);
  212.         au_printf_c(au, 15, "Matches        = %d\n", in->number_matched);
  213.     }
  214.  
  215.     return 0;
  216. }
  217.  
  218.